#include <vector>
#include <iostream>

using namespace std;

class Solution 
{
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) 
    {
        if(matrix.empty())
        {
            return {};
        }
        int direction = 0;
        int rowCount = matrix.size();
        int columnCount = matrix[0].size();
        vector<int> result(rowCount * columnCount);
        int up = 0;
        int down = rowCount - 1;
        int left = 0;
        int right = columnCount - 1;
        int number = 0;
        while(1)
        {
            if(number == result.size())
            {
                break;
            }
            if(direction == 0)
            {
                for(int i = left; i <= right; i++)
                {
                    result[number] = matrix[up][i];
                    number++;
                }
                up++;
                direction = 1;
            }
            else if(direction == 1)
            {
                for(int i = up; i <= down; i++)
                {
                    result[number] = matrix[i][right];
                    number++;
                }
                right--;
                direction = 2;
            }
            else if(direction == 2)
            {
                for(int i = right; i >= left; i--)
                {
                    result[number] = matrix[down][i];
                    number++;
                }
                down--;
                direction = 3;
            }
            else if(direction == 3)
            {
                for(int i = down; i >= up; i--)
                {
                    result[number] = matrix[i][left];
                    number++;
                }
                left++;
                direction = 0;
            }
        }
        return result;
    }
};

// 辅助函数：打印数组
void printArray(const vector<int>& nums) 
{
    cout << "[";
    for(int i = 0; i < nums.size(); i++) 
    {
        cout << nums[i];
        if(i < nums.size() - 1) cout << ",";
    }
    cout << "]";
}

// 辅助函数：打印矩阵
void printMatrix(const vector<vector<int>>& matrix) 
{
    cout << "[";
    for(int i = 0; i < matrix.size(); i++) 
    {
        cout << "[";
        for(int j = 0; j < matrix[i].size(); j++) 
        {
            cout << matrix[i][j];
            if(j < matrix[i].size() - 1) cout << ",";
        }
        cout << "]";
        if(i < matrix.size() - 1) cout << ",";
    }
    cout << "]";
}

int main() 
{
    Solution solution;
    
    // 测试案例1: [[1,2,3],[4,5,6],[7,8,9]]
    cout << "测试案例1:" << endl;
    vector<vector<int>> matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    cout << "输入: matrix = ";
    printMatrix(matrix1);
    cout << endl;
    vector<int> result1 = solution.spiralOrder(matrix1);
    cout << "输出: ";
    printArray(result1);
    cout << endl;
    cout << "期望: [1,2,3,6,9,8,7,4,5]" << endl << endl;
    
    // 测试案例2: [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
    cout << "测试案例2:" << endl;
    vector<vector<int>> matrix2 = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
    cout << "输入: matrix = ";
    printMatrix(matrix2);
    cout << endl;
    vector<int> result2 = solution.spiralOrder(matrix2);
    cout << "输出: ";
    printArray(result2);
    cout << endl;
    cout << "期望: [1,2,3,4,8,12,11,10,9,5,6,7]" << endl << endl;
    
    // 测试案例3: 单行矩阵
    cout << "测试案例3 (单行矩阵):" << endl;
    vector<vector<int>> matrix3 = {{1, 2, 3, 4, 5}};
    cout << "输入: matrix = ";
    printMatrix(matrix3);
    cout << endl;
    vector<int> result3 = solution.spiralOrder(matrix3);
    cout << "输出: ";
    printArray(result3);
    cout << endl;
    cout << "期望: [1,2,3,4,5]" << endl << endl;
    
    // 测试案例4: 单列矩阵
    cout << "测试案例4 (单列矩阵):" << endl;
    vector<vector<int>> matrix4 = {{1}, {2}, {3}, {4}};
    cout << "输入: matrix = ";
    printMatrix(matrix4);
    cout << endl;
    vector<int> result4 = solution.spiralOrder(matrix4);
    cout << "输出: ";
    printArray(result4);
    cout << endl;
    cout << "期望: [1,2,3,4]" << endl << endl;
    
    // 测试案例5: 单元素矩阵
    cout << "测试案例5 (单元素矩阵):" << endl;
    vector<vector<int>> matrix5 = {{1}};
    cout << "输入: matrix = ";
    printMatrix(matrix5);
    cout << endl;
    vector<int> result5 = solution.spiralOrder(matrix5);
    cout << "输出: ";
    printArray(result5);
    cout << endl;
    cout << "期望: [1]" << endl << endl;
    
    // 测试案例6: 2x2矩阵
    cout << "测试案例6 (2x2矩阵):" << endl;
    vector<vector<int>> matrix6 = {{1, 2}, {3, 4}};
    cout << "输入: matrix = ";
    printMatrix(matrix6);
    cout << endl;
    vector<int> result6 = solution.spiralOrder(matrix6);
    cout << "输出: ";
    printArray(result6);
    cout << endl;
    cout << "期望: [1,2,4,3]" << endl << endl;
    
    // 测试案例7: 1x4矩阵
    cout << "测试案例7 (1x4矩阵):" << endl;
    vector<vector<int>> matrix7 = {{1, 2, 3, 4}};
    cout << "输入: matrix = ";
    printMatrix(matrix7);
    cout << endl;
    vector<int> result7 = solution.spiralOrder(matrix7);
    cout << "输出: ";
    printArray(result7);
    cout << endl;
    cout << "期望: [1,2,3,4]" << endl << endl;
    
    // 测试案例8: 4x1矩阵
    cout << "测试案例8 (4x1矩阵):" << endl;
    vector<vector<int>> matrix8 = {{1}, {2}, {3}, {4}};
    cout << "输入: matrix = ";
    printMatrix(matrix8);
    cout << endl;
    vector<int> result8 = solution.spiralOrder(matrix8);
    cout << "输出: ";
    printArray(result8);
    cout << endl;
    cout << "期望: [1,2,3,4]" << endl << endl;
    
    // 测试案例9: 5x5矩阵
    cout << "测试案例9 (5x5矩阵):" << endl;
    vector<vector<int>> matrix9 = {
        {1, 2, 3, 4, 5},
        {6, 7, 8, 9, 10},
        {11, 12, 13, 14, 15},
        {16, 17, 18, 19, 20},
        {21, 22, 23, 24, 25}
    };
    cout << "输入: matrix = ";
    printMatrix(matrix9);
    cout << endl;
    vector<int> result9 = solution.spiralOrder(matrix9);
    cout << "输出: ";
    printArray(result9);
    cout << endl;
    cout << "期望: [1,2,3,4,5,10,15,20,25,24,23,22,21,16,11,6,7,8,9,14,19,18,17,12,13]" << endl << endl;
    
    // 测试案例10: 2x3矩阵
    cout << "测试案例10 (2x3矩阵):" << endl;
    vector<vector<int>> matrix10 = {{1, 2, 3}, {4, 5, 6}};
    cout << "输入: matrix = ";
    printMatrix(matrix10);
    cout << endl;
    vector<int> result10 = solution.spiralOrder(matrix10);
    cout << "输出: ";
    printArray(result10);
    cout << endl;
    cout << "期望: [1,2,3,6,5,4]" << endl << endl;
    
    return 0;
}